package mithrilwings.game;

import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import fifis.game.FLogic;
import org.apache.log4j.Logger;
//import java.util.ArrayList;

/**
 *
 * @author porfiriopartida
 */
public class MithrilWingsLogic extends FLogic {
    final static protected Logger log4j = Logger.getLogger(MithrilWingsLogic.class);

    public void run() {
//        Date start = new Date();
//        while(true){
//            try {
//                Date now = new Date();
//                if (now.getTime() - start.getTime() > 10000l) {
//                    log4j.debug("SE ACABÓ EL JUEGO");
//                    break;
//                }
//                Thread.sleep(1000);
//            } catch (Exception e) {
//                e.printStackTrace();
//                continue;
//            }
//        }
//        Common.getInstance().getRootNode().detachAllChildren();
    }

    public void update() {
    }

    public void collision(PhysicsCollisionEvent event) {
        if (MainMithrilWings.me == null) {
            return;
        }
        Spatial a = event.getNodeA();
        Spatial b = event.getNodeB();
        Node scenarioNode = getScenario().getNode();
        boolean collidedWithScenario = a.equals(scenarioNode) || b.equals(scenarioNode);
        if (!collidedWithScenario) {
//            log4j.debug("Something is colliding, but it is not the scenario");
            return;
        }
        Spatial scenario = null;
        Node other = null;
//        ShipController fActor = null;
        if (a.equals(scenarioNode)) {
            scenario = a;
            other = (Node) b;
        } else {
            other = (Node) a;
            scenario = b;
        }

        if (other.getName().equals(MainMithrilWings.me.getGhostName())) {
            MainMithrilWings.me.collided();
        }

////        ArrayList<FController> actors = getActors();
//        Collection<FController> actors = getActors();
//        for (FController actor : actors) {
////            if (other.getName().equals(actor.getNode().getName())) {
////            if (other.equals(actor.getNode().getChild("ghostNode"))) {
//            if (other.getName().equals("ghostNode")) {
//                col = false;
//                fActor = (ShipController) actor;
//                break;
//            }
//        }
    }
}
